﻿/* 
 * Saint Aside - Come to Me AR Music Video
 * ----------------------------------------
 * Copyright 2010 (c) Peter Oberlaender, augmedia.de, wayoutofline.net
 * visit: http://ar.saintaside.com
 * Published under the GNU GENERAL PUBLIC LICENSE v2.
 * mailto: pete@wayoutofline.net
 */
 package {
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.media.Video;
    import flash.net.NetConnection;
    import flash.net.NetStream;
    import flash.utils.ByteArray;
    import flash.utils.*;
    import flash.display.*;
    import flash.events.*;
    import flash.net.*;
    import flash.ui.*;
    import fl.controls.Button;
    import flash.filters.BlurFilter;
    import flash.filters.ColorMatrixFilter;
    import flash.display.Bitmap;
    import flash.geom.Point;
    import flash.media.Sound;
    import org.libspark.flartoolkit.core.transmat.FLARTransMatResult;
    import org.libspark.flartoolkit.support.pv3d.FLARBaseNode;
    import org.libspark.flartoolkit.support.pv3d.FLARCamera3D;
    import org.libspark.flartoolkit.detector.FLARSingleMarkerDetector;
    import org.papervision3d.render.LazyRenderEngine;
    import org.papervision3d.scenes.Scene3D;
    import org.papervision3d.view.Viewport3D;
    import org.papervision3d.view.stats.StatsView;
    import org.papervision3d.materials.MovieMaterial;
    import org.papervision3d.objects.DisplayObject3D;
    import org.papervision3d.lights.PointLight3D;
    import org.papervision3d.materials.WireframeMaterial;
    import org.papervision3d.materials.shadematerials.FlatShadeMaterial;
    import org.papervision3d.materials.VideoStreamMaterial;
    import org.papervision3d.materials.utils.MaterialsList;
    import org.papervision3d.materials.*;
    import org.papervision3d.objects.primitives.Cube;
    import org.papervision3d.objects.primitives.Plane;
    import org.papervision3d.objects.primitives.Sphere;
    import org.papervision3d.objects.parsers.DAE;
    import org.papervision3d.objects.parsers.Collada;
    import org.papervision3d.materials.BitmapFileMaterial;
    import org.papervision3d.events.FileLoadEvent;
    import org.papervision3d.core.animation.clip.AnimationClip3D;	
    import org.papervision3d.view.BasicView;
    import org.papervision3d.materials.ColorMaterial;
    import org.papervision3d.core.math.Matrix3D;
    import org.papervision3d.core.math.Number3D;
    import org.papervision3d.materials.shaders.PhongShader;
    import org.papervision3d.materials.shaders.ShadedMaterial;
    import org.papervision3d.materials.BitmapMaterial;
    import org.papervision3d.materials.shadematerials.*;
    import org.papervision3d.materials.shaders.*;
    import org.papervision3d.view.layer.ViewportLayer;
    import org.papervision3d.view.layer.util.ViewportLayerSortMode;
    import org.papervision3d.materials.MovieAssetMaterial;
    import org.papervision3d.core.utils.virtualmouse.VirtualMouse;
    import caurina.transitions.Tweener;
    import caurina.transitions.properties.FilterShortcuts;
    import caurina.transitions.properties.ColorShortcuts;
    import caurina.transitions.properties.CurveModifiers;
    import caurina.transitions.properties.DisplayShortcuts;
    import caurina.transitions.properties.SoundShortcuts;
    import caurina.transitions.properties.TextShortcuts;
    import com.peteroberlaender.pv3d.materials.*;
    [SWF(width=640, height=480, backgroundColor=0x808080, frameRate=30)]
    public class SAAC extends ARAppBase {
        protected var _base:Sprite;
        protected var _viewport:Viewport3D;
        protected var _camera3d:FLARCamera3D;
        protected var _scene:Scene3D;
        protected var _renderer:LazyRenderEngine;
        protected var _markerNode:FLARBaseNode;
        private var transformationResult:FLARTransMatResult;
        private var _modelWRAP : DisplayObject3D;
        private var _lastRot : Number3D;
        private var material1:FlatShadeMaterial;
        private var material3:BitmapFileMaterial;
        private var material4:VideoStreamMaterial;
        private var quality:uint = 16;
        private var netConnection:NetConnection;
        private var video:Video;
        private var audio;
        private var netStream:NetStream;
        private var video_cube:Cube;
        private var videoMat:VideoStreamMaterial;
        private var mouthContainer:DisplayObject3D;
        private var videoContainer:DisplayObject3D;
        private const EASE:Number=0.4;
        private var obRotation:Number3D;
        private var obPosition:Number3D;
        private var tongue:DisplayObject3D;
        private var prevMouthX:Number=0;
        private var prevMouthY:Number=0;
        private var prevMouthZ:Number=0;
        private var counta:Number = 0;
        private var theV_1:int=0;
        private var theV_2:int=1;
        private var theV_3:int=0;
        private var materialsList:MaterialsList;
        private var bitmapMaterial:BitmapFileMaterial;
        private var shadedMaterial:ShadedMaterial;
        private var shader:PhongShader;
        private var primitive:Plane;
        private var LIGHT_Beam1:BitmapFileMaterial;
        private var STAGE_Truss:BitmapFileMaterial;
        private var STAGE_Teppich:BitmapFileMaterial;
        private var STAGE_Ground:BitmapFileMaterial;
        private var STAGE_Wall:BitmapFileMaterial;
        private var STAGE_WallFrame:BitmapFileMaterial;
        private var STAGE_Sky:BitmapFileMaterial;
        private var STAGE_SkyBlock:ColorMaterial;
        private var STAGE_Riser:BitmapFileMaterial;
        private var AMPs_Crate2:BitmapFileMaterial;
        private var AMPs_Crate_Top:BitmapFileMaterial;
        private var AMPs_Marshall:BitmapFileMaterial;
        private var AMPs_Marshall_Top2:BitmapFileMaterial;
        private var AMPs_Framus:BitmapFileMaterial;
        private var AMPs_Framus_Top2:BitmapFileMaterial;
        private var AMPs_Monitor:BitmapFileMaterial;
        private var AMPs_Lamp:BitmapFileMaterial;
        private var lightBeam_1:DisplayObject3D;
        private var lightBeam_2:DisplayObject3D;
        private var lightBeam_3:DisplayObject3D;
        private var lightBeam_4:DisplayObject3D;
        private var lightBeam_5:DisplayObject3D;
        private var lightBeam_6:DisplayObject3D;
        private var truss1:DisplayObject3D;
        private var truss2:DisplayObject3D;
        private var truss3:DisplayObject3D;
        private var truss4:DisplayObject3D;
        private var truss5:DisplayObject3D;
        private var truss6:DisplayObject3D;
        private var truss7:DisplayObject3D;
        private var truss8:DisplayObject3D;
        private var stageArri1:DisplayObject3D;
        private var stageArri2:DisplayObject3D;
        private var stageArriDoor1:DisplayObject3D;
        private var stageArriDoor2:DisplayObject3D;
        private var stageArriBlue1:DisplayObject3D;
        private var stageArriBlue2:DisplayObject3D;
        private var stageArriBack1:DisplayObject3D;
        private var stageArriBack2:DisplayObject3D;
        private var par1:DisplayObject3D;
        private var par2:DisplayObject3D;
        private var par3:DisplayObject3D;
        private var par4:DisplayObject3D;
        private var ground1:DisplayObject3D;		
        private var teppich1:DisplayObject3D;
        private var wall1:DisplayObject3D;
        private var wallFrame1:DisplayObject3D;
        private var stageSky1:DisplayObject3D;
        private var stageSkyBlock1:DisplayObject3D;
        private var wingBucker1:DisplayObject3D;
        private var lampstick1:DisplayObject3D;
        private var lamphead1:DisplayObject3D;
        private var monitor31:DisplayObject3D;
        private var monitor21:DisplayObject3D;
        private var monitor11:DisplayObject3D;
        private var crateT1:DisplayObject3D;
        private var crateB1:DisplayObject3D;
        private var marshallT1:DisplayObject3D;
        private var marshallB1:DisplayObject3D;
        private var framusT1:DisplayObject3D;
        private var framusB1:DisplayObject3D;
        private var screenback1:DisplayObject3D;
        private var screenfront1:DisplayObject3D;
        private var riser1:DisplayObject3D;
        public var smooth:Boolean;
        public var opposite:Boolean;
        public var invisible:Boolean;
        public var viewportLayer:ViewportLayer;
        private var _amps:DAE;
        private var headLights:ViewportLayer;
        private var headLights2:ViewportLayer;
        private var headLights3:ViewportLayer;
        private var markerCube:ViewportLayer;
        private var theskies:ViewportLayer;
        private var restOfStage:ViewportLayer;
        private var theWall:ViewportLayer;
        private var AdOneLink:ViewportLayer;
        private var AdTwoLink:ViewportLayer;
        private var _cube:Cube;
        private var screenMc:Plane;
        private var screenMcMat:MovieAssetMaterial;
        private var AdOneMc:Plane;
        private var AdOneMcMat:MovieAssetMaterial;
        private var AdTwoMc:Plane;
        private var AdTwoMcMat:MovieAssetMaterial;
        protected override function init(cameraFile:String, codeFile:String, canvasWidth:int = 320, canvasHeight:int = 240, codeWidth:int = 80):void {
        addEventListener(Event.INIT, _onInit, false, int.MAX_VALUE);
        super.init(cameraFile, codeFile, canvasWidth, canvasHeight, codeWidth);
        }
        public function SAAC() {
            addEventListener(Event.INIT, _onInit);
            init('Data/camera_para.dat', 'Data/saintaside.pat');
            FilterShortcuts.init();
            ColorShortcuts.init();
        }
        private function _onInit(e:Event):void {
        mouthContainer=new DisplayObject3D();
        videoContainer=new DisplayObject3D();
        _modelWRAP = new DisplayObject3D();
        _base = addChild(new Sprite()) as Sprite;
        _capture.width = 640;
        _capture.height = 480;
        _base.addChild(_capture);
        _viewport = _base.addChild(new Viewport3D(320, 240)) as Viewport3D;
        _viewport.containerSprite.sortMode = ViewportLayerSortMode.ORIGIN_SORT;
        _viewport.scaleX = 640 / 320;
        _viewport.scaleY = 480 / 240;
        _camera3d = new FLARCamera3D(_param);
        _scene = new Scene3D();
        _markerNode = _scene.addChild(new FLARBaseNode()) as FLARBaseNode;
        _renderer = new LazyRenderEngine(_scene, _camera3d, _viewport);
        transformationResult = new FLARTransMatResult();
        var light:PointLight3D = new PointLight3D(true, true);
        light.x = 0;
        light.y = 1000;
        light.z = -1000;
        var customClient:Object = new Object();
        netConnection = new NetConnection();
        netConnection.connect(null); 
        netStream = new NetStream(netConnection);
        netStream.client = customClient;
        netStream.bufferTime = 20;
        video = new Video();
        video.smoothing = true;
        video.attachNetStream(netStream);
        netStream.play("SAACcontent/complete7.flv");
        netStream.pause();
        videoMat = new VideoStreamMaterial(video, netStream, true, true);
        videoMat.smooth = true;
        videoMat.doubleSided = false;						
        LIGHT_Beam1 = new BitmapFileMaterial("SAACcontent/LIGHT_Beam1.png");
        STAGE_Ground = new BitmapFileMaterial("SAACcontent/STAGE_Ground.jpg");
        STAGE_Wall = new BitmapFileMaterial("SAACcontent/STAGE_Wall.png");
        STAGE_WallFrame = new BitmapFileMaterial("SAACcontent/STAGE_WallFrame.png");
        STAGE_Sky = new BitmapFileMaterial("SAACcontent/STAGE_Sky.jpg");
        STAGE_Sky.opposite = true;
        STAGE_SkyBlock = new ColorMaterial(0x00ff00);
        STAGE_Truss = new BitmapFileMaterial("SAACcontent/STAGE_Truss.png");
        STAGE_Truss.doubleSided = true;
        STAGE_Riser = new BitmapFileMaterial("SAACcontent/STAGE_Riser.png");
        STAGE_Riser.doubleSided = true;
        AMPs_Crate2 = new BitmapFileMaterial("SAACcontent/AMPs_Crate_l.jpg");
        AMPs_Crate2.opposite = true;
        AMPs_Crate_Top = new BitmapFileMaterial("SAACcontent/AMPs_Crate_top_l.jpg");
        AMPs_Marshall = new BitmapFileMaterial("SAACcontent/AMPs_Marshall_l.jpg");
        AMPs_Marshall_Top2 = new BitmapFileMaterial("SAACcontent/AMPs_Marshall_top_l2.jpg");
        AMPs_Marshall_Top2.opposite = true;
        AMPs_Framus = new BitmapFileMaterial("SAACcontent/AMPs_framus_l.jpg");
        AMPs_Framus_Top2 = new BitmapFileMaterial("SAACcontent/AMPs_framus_top_l.jpg");
        AMPs_Framus_Top2.opposite = true;
        AMPs_Monitor = new BitmapFileMaterial("SAACcontent/AMPs_Monitor_l.jpg");
        AMPs_Lamp = new BitmapFileMaterial("SAACcontent/AMPs_Lamp_l.jpg"); 
        var matList=new MaterialsList();
        matList.addMaterial(AMPs_Crate2,"AMPs_Crate");
        matList.addMaterial(AMPs_Crate_Top,"AMPs_Crate_Top");
        matList.addMaterial(AMPs_Marshall,"AMPs_Marshall");
        matList.addMaterial(AMPs_Marshall_Top2,"AMPs_Marshall_Top");
        matList.addMaterial(AMPs_Framus,"AMPs_Framus");
        matList.addMaterial(AMPs_Framus_Top2,"AMPs_Framus_Top");
        matList.addMaterial(AMPs_Monitor,"AMPs_Monitor");
        matList.addMaterial(AMPs_Lamp,"AMPs_Lamp");
        matList.addMaterial(STAGE_Ground,"STAGE_Ground");
        matList.addMaterial(STAGE_Wall,"STAGE_Wall");
        matList.addMaterial(STAGE_WallFrame,"STAGE_WallFrame");
        matList.addMaterial(STAGE_Sky,"STAGE_Sky");
        matList.addMaterial(STAGE_SkyBlock,"STAGE_SkyBlock");
        matList.addMaterial(STAGE_Truss,"STAGE_Truss");
        matList.addMaterial(STAGE_Riser,"STAGE_Riser");
        matList.addMaterial(LIGHT_Beam1,"LIGHT_Beam1");
        matList.addMaterial(videoMat,"Material5");
        matList.addMaterial(videoMat,"Material4");
        _amps = new DAE(false, "_amps", false);
        _amps.load('SAACcontent/STAGE_88.dae', matList);
        _amps.addEventListener(FileLoadEvent.LOAD_COMPLETE,onAmpsLoaded);
        _amps.scale = 0.07;
        _amps.rotationX = 90;
        _amps.rotationZ = 180;
        _amps.z = 0;
        _amps.x -= 0;
        _amps.y -= 0;
        var _cubefront:BitmapFileMaterial = new BitmapFileMaterial("SAACcontent/saintasidemarker.jpg");
        var _cuberest:FlatShadeMaterial = new FlatShadeMaterial(light, 0x555555, 0xcccccc);
        _cube = new Cube(new MaterialsList({front: _cubefront, back: _cuberest, right: _cuberest, left: _cuberest, top: _cuberest, bottom: _cuberest}), 90, 1, 90);
        _cube.x -= -4;
        _cube.y -= 0;
        _cube.z = 0;
        screenMcMat = new MovieAssetMaterial("com.peteroberlaender.pv3d.materials.SAACARMC", true, true, true);
        screenMcMat.animated=true;
        screenMcMat.smooth=true;
        screenMcMat.interactive = true;
        screenMc=new Plane(screenMcMat,175,80,3,3);
        screenMc.rotationX = 180;
        screenMc.rotationZ = 180;
        screenMc.x = 2-95;
        screenMc.y = 6.5+110;
        screenMc.z = 7.5;
        AdOneMcMat = new MovieAssetMaterial("com.peteroberlaender.pv3d.materials.SAACARAD1MC", true, true, true);
        AdOneMcMat.animated=true;
        AdOneMcMat.interactive = true;
        AdOneMc=new Plane(AdOneMcMat,95,140,3,3);
        AdOneMc.rotationX=180;
        AdOneMc.rotationZ=180;				
        AdOneMc.x = -182-95-3;
        AdOneMc.y = +110;
        AdOneMc.z = 0;
        AdTwoMcMat = new MovieAssetMaterial("com.peteroberlaender.pv3d.materials.SAACARAD2MC", true, true, true);
        AdTwoMcMat.animated=true;
        AdTwoMcMat.interactive = true;
        AdTwoMc=new Plane(AdTwoMcMat,95,140,3,3);
        AdTwoMc.rotationX=180;
        AdTwoMc.rotationZ=180;				
        AdTwoMc.x = 182-95+3;
        AdTwoMc.y = +110;
        AdTwoMc.z = 0;
        _modelWRAP.addChild( screenMc );
        _modelWRAP.addChild( _cube );
        _modelWRAP.addChild( AdOneMc );
        _modelWRAP.addChild( AdTwoMc );
        _markerNode.addChild( _modelWRAP );
        mirror = !mirror;
        this.start( );
        addEventListener(Event.ENTER_FRAME, _update);
        }
        private function onAmpsLoaded(e:FileLoadEvent):void
        {
            if (theV_2 == 1) {
            trace(theV_2);
            theV_2 = 4663;
            lightBeam_1 = _amps.getChildByName("lightBeam1", true);
            lightBeam_2 = _amps.getChildByName("lightBeam2", true);
            lightBeam_3 = _amps.getChildByName("lightBeam3", true);
            lightBeam_4 = _amps.getChildByName("lightBeam4", true);
            lightBeam_5 = _amps.getChildByName("lightBeam5", true);
            lightBeam_6 = _amps.getChildByName("lightBeam6", true);
            truss1 = _amps.getChildByName("stageTruss", true);
            truss2 = _amps.getChildByName("stageTruss2", true);	
            truss3 = _amps.getChildByName("stageTruss3", true);
            truss4 = _amps.getChildByName("stageTruss4", true);
            truss5 = _amps.getChildByName("stageTruss5", true);
            truss6 = _amps.getChildByName("stageTruss6", true);
            truss7 = _amps.getChildByName("stageTruss7", true);
            truss8 = _amps.getChildByName("stageTruss8", true);
            stageArri1 = _amps.getChildByName("stageArri", true);
            stageArri2 = _amps.getChildByName("stageArri2", true);
            stageArriDoor1 = _amps.getChildByName("stageArriDoor", true);
            stageArriDoor2 = _amps.getChildByName("stageArriDoor2", true);
            stageArriBlue1 = _amps.getChildByName("stageArriBlue", true);
            stageArriBlue2 = _amps.getChildByName("stageArriBlue2", true);
            stageArriBack1 = _amps.getChildByName("stageArriBack", true);
            stageArriBack2 = _amps.getChildByName("stageArriBack2", true);	
            par1 = _amps.getChildByName("stagePar1", true);
            par2 = _amps.getChildByName("stagePar2", true);
            par3 = _amps.getChildByName("stagePar3", true);
            par4 = _amps.getChildByName("stagePar4", true);
            ground1 = _amps.getChildByName("ground", true);
            wall1 = _amps.getChildByName("wall", true );
            wallFrame1 = _amps.getChildByName("wallFrame", true );
            stageSky1 = _amps.getChildByName("stageSky", true);
            stageSkyBlock1 = _amps.getChildByName("stageSkyBlock", true);
            wingBucker1 = _amps.getChildByName("wingBucker", true);
            lampstick1 = _amps.getChildByName("lampstick", true );
            lamphead1 =  _amps.getChildByName("lamphead", true );
            monitor31 =  _amps.getChildByName("monitor3", true );
            monitor21 =  _amps.getChildByName("monitor2", true );
            monitor11 =  _amps.getChildByName("monitor1", true );
            crateT1 =  _amps.getChildByName("crateT", true );
            crateB1 =  _amps.getChildByName("crateB", true );
            marshallT1 =  _amps.getChildByName("marshallT", true );
            marshallB1 =  _amps.getChildByName("marshallB", true );
            framusT1 =  _amps.getChildByName("framusT", true );
            framusB1 =  _amps.getChildByName("framusB", true );
            screenback1 =  _amps.getChildByName("screenback", true );
            screenfront1 =  _amps.getChildByName("screenfront", true );
            riser1 =  _amps.getChildByName("stageRiser", true );
            _viewport.containerSprite.sortMode = ViewportLayerSortMode.INDEX_SORT;
            restOfStage = new ViewportLayer(_viewport,null);
            _viewport.containerSprite.addLayer(restOfStage);
            restOfStage.addDisplayObject3D(stageArri1);
            restOfStage.addDisplayObject3D(stageArri2);
            restOfStage.addDisplayObject3D(stageArriDoor1);
            restOfStage.addDisplayObject3D(stageArriDoor2);
            restOfStage.addDisplayObject3D(stageArriBlue1);
            restOfStage.addDisplayObject3D(stageArriBlue2);
            restOfStage.addDisplayObject3D(stageArriBack1);
            restOfStage.addDisplayObject3D(stageArriBack2);	
            restOfStage.addDisplayObject3D(par1);
            restOfStage.addDisplayObject3D(par2);
            restOfStage.addDisplayObject3D(par3);
            restOfStage.addDisplayObject3D(par4);
            restOfStage.addDisplayObject3D(truss3);
            restOfStage.addDisplayObject3D(truss4);
            restOfStage.addDisplayObject3D(truss5);
            restOfStage.addDisplayObject3D(truss6);
            restOfStage.addDisplayObject3D(truss7);
            restOfStage.addDisplayObject3D(truss8);
            restOfStage.addDisplayObject3D(truss1);
            restOfStage.addDisplayObject3D(truss2);	
            restOfStage.addDisplayObject3D(lampstick1);
            restOfStage.addDisplayObject3D(lamphead1);
            restOfStage.addDisplayObject3D(monitor31);
            restOfStage.addDisplayObject3D(monitor21);
            restOfStage.addDisplayObject3D(monitor11);
            restOfStage.addDisplayObject3D(crateT1);
            restOfStage.addDisplayObject3D(crateB1);
            restOfStage.addDisplayObject3D(marshallT1);
            restOfStage.addDisplayObject3D(marshallB1);
            restOfStage.addDisplayObject3D(framusT1);
            restOfStage.addDisplayObject3D(framusB1);
            restOfStage.addDisplayObject3D(screenback1);
            restOfStage.addDisplayObject3D(screenfront1);
            restOfStage.addDisplayObject3D(ground1);
            restOfStage.addDisplayObject3D(wingBucker1);	
            restOfStage.addDisplayObject3D(riser1);
            theWall = new ViewportLayer(_viewport,null);
            _viewport.containerSprite.addLayer(theWall);
            theWall.addDisplayObject3D(wall1);
            theWall.addDisplayObject3D(screenMc);
            headLights = new ViewportLayer(_viewport,null);
            _viewport.containerSprite.addLayer(headLights);
            headLights.addDisplayObject3D(lightBeam_1);headLights.addDisplayObject3D(lightBeam_2);
            headLights2 = new ViewportLayer(_viewport,null);
            _viewport.containerSprite.addLayer(headLights2);
            headLights2.addDisplayObject3D(lightBeam_3);headLights2.addDisplayObject3D(lightBeam_5);
            headLights3 = new ViewportLayer(_viewport,null);
            _viewport.containerSprite.addLayer(headLights3);
            headLights3.addDisplayObject3D(lightBeam_4);headLights3.addDisplayObject3D(lightBeam_6);
            theskies = new ViewportLayer(_viewport,null);
            _viewport.containerSprite.addLayer(theskies);
            theskies.addDisplayObject3D(stageSky1);
            theskies.addDisplayObject3D(stageSkyBlock1);
            theskies.addDisplayObject3D(wallFrame1);
            theskies.filters = [
            new ColorMatrixFilter([
            1, 0, 0, 0, 0,
            0, 1, 0, 0, 0,
            0, 0, 1, 0, 0,
            1, -1, 1, 1, 0
                ])
            ];
            markerCube = new ViewportLayer(_viewport,null);
            _viewport.containerSprite.addLayer(markerCube);
            markerCube.addDisplayObject3D(_cube);
            AdOneLink = new ViewportLayer(_viewport,null);
            _viewport.containerSprite.addLayer(AdOneLink);
            AdOneLink.addDisplayObject3D(AdOneMc);
            AdOneLink.buttonMode = true;
            AdOneLink.useHandCursor = true;
            AdOneLink.addEventListener(MouseEvent.CLICK, AdOneMcClick);
            AdTwoLink = new ViewportLayer(_viewport,null);
            _viewport.containerSprite.addLayer(AdTwoLink);
            AdTwoLink.addDisplayObject3D(AdTwoMc);
            AdTwoLink.buttonMode = true;
            AdTwoLink.useHandCursor = true;
            AdTwoLink.addEventListener(MouseEvent.CLICK, AdTwoMcClick);
            restOfStage.layerIndex = 8;
            headLights.layerIndex = 7;
            headLights2.layerIndex = 7;
            headLights3.layerIndex = 7;
            theWall.layerIndex = 2;
            theskies.layerIndex = 1;
            markerCube.layerIndex = 0;
            AdOneLink.layerIndex = 0;
            AdTwoLink.layerIndex = 0;
            lightBeam_1.useOwnContainer = true;
            lightBeam_2.useOwnContainer = true;
            lightBeam_3.useOwnContainer = true;
            lightBeam_4.useOwnContainer = true;
            lightBeam_5.useOwnContainer = true;
            lightBeam_6.useOwnContainer = true;
            truss1.useOwnContainer = true;
            truss2.useOwnContainer = true;
            truss3.useOwnContainer = true;
            truss4.useOwnContainer = true;
            truss5.useOwnContainer = true;
            truss6.useOwnContainer = true;
            truss7.useOwnContainer = true;
            truss8.useOwnContainer = true;
            stageArri1.useOwnContainer = true;
            stageArri2.useOwnContainer = true;
            stageArriDoor1.useOwnContainer = true;
            stageArriDoor2.useOwnContainer = true;
            stageArriBlue1.useOwnContainer = true;
            stageArriBlue2.useOwnContainer = true;
            stageArriBack1.useOwnContainer = true;
            stageArriBack2.useOwnContainer = true;
            par1.useOwnContainer = true;
            par2.useOwnContainer = true;
            par3.useOwnContainer = true;
            par4.useOwnContainer = true;		
            ground1.useOwnContainer = true;
            lamphead1.useOwnContainer = true;
            monitor31.useOwnContainer = true;
            monitor21.useOwnContainer = true;
            monitor11.useOwnContainer = true;
            crateT1.useOwnContainer = true;
            crateB1.useOwnContainer = true;
            marshallT1.useOwnContainer = true;
            marshallB1.useOwnContainer = true;
            framusT1.useOwnContainer = true;
            framusB1.useOwnContainer = true;
            screenback1.useOwnContainer = true;
            screenfront1.useOwnContainer = true;
            riser1.useOwnContainer = true;
            lightBeam_1.meshSort = DisplayObject3D.MESH_SORT_FAR;
            lightBeam_2.meshSort = DisplayObject3D.MESH_SORT_FAR;
            lightBeam_3.meshSort = DisplayObject3D.MESH_SORT_FAR;
            lightBeam_4.meshSort = DisplayObject3D.MESH_SORT_FAR;
            lightBeam_5.meshSort = DisplayObject3D.MESH_SORT_FAR;
            lightBeam_6.meshSort = DisplayObject3D.MESH_SORT_FAR;
            truss1.meshSort = DisplayObject3D.MESH_SORT_FAR;
            truss2.meshSort = DisplayObject3D.MESH_SORT_FAR;
            truss3.meshSort = DisplayObject3D.MESH_SORT_FAR;
            truss4.meshSort = DisplayObject3D.MESH_SORT_FAR;
            truss5.meshSort = DisplayObject3D.MESH_SORT_FAR;
            truss6.meshSort = DisplayObject3D.MESH_SORT_FAR;
            truss7.meshSort = DisplayObject3D.MESH_SORT_FAR;
            truss8.meshSort = DisplayObject3D.MESH_SORT_FAR;
            stageArri1.meshSort = DisplayObject3D.MESH_SORT_FAR;
            stageArri2.meshSort = DisplayObject3D.MESH_SORT_FAR;
            stageArriDoor1.meshSort = DisplayObject3D.MESH_SORT_FAR;
            stageArriDoor2.meshSort = DisplayObject3D.MESH_SORT_FAR;
            stageArriBlue1.meshSort = DisplayObject3D.MESH_SORT_FAR;
            stageArriBlue2.meshSort = DisplayObject3D.MESH_SORT_FAR;
            stageArriBack1.meshSort = DisplayObject3D.MESH_SORT_FAR;
            stageArriBack2.meshSort = DisplayObject3D.MESH_SORT_FAR;
            par1.meshSort = DisplayObject3D.MESH_SORT_FAR;
            par2.meshSort = DisplayObject3D.MESH_SORT_FAR;
            par3.meshSort = DisplayObject3D.MESH_SORT_FAR;
            par4.meshSort = DisplayObject3D.MESH_SORT_FAR;
            ground1.meshSort = DisplayObject3D.MESH_SORT_FAR;
            lampstick1.meshSort = DisplayObject3D.MESH_SORT_FAR;
            lamphead1.meshSort = DisplayObject3D.MESH_SORT_FAR;
            monitor31.meshSort = DisplayObject3D.MESH_SORT_FAR;
            monitor21.meshSort = DisplayObject3D.MESH_SORT_FAR;
            monitor11.meshSort = DisplayObject3D.MESH_SORT_FAR;
            crateT1.meshSort = DisplayObject3D.MESH_SORT_FAR;
            crateB1.meshSort = DisplayObject3D.MESH_SORT_FAR;
            marshallT1.meshSort = DisplayObject3D.MESH_SORT_FAR;
            marshallB1.meshSort = DisplayObject3D.MESH_SORT_FAR;
            framusT1.meshSort = DisplayObject3D.MESH_SORT_FAR;
            framusB1.meshSort = DisplayObject3D.MESH_SORT_FAR;
            screenback1.meshSort = DisplayObject3D.MESH_SORT_FAR;
            screenfront1.meshSort = DisplayObject3D.MESH_SORT_FAR;
            riser1.meshSort = DisplayObject3D.MESH_SORT_FAR;
            addEventListener(Event.ENTER_FRAME, _onEnterFrame);
            }
        }
        private function _update(e:Event):void {
            var netClient:Object = new Object();
            if (netStream.time>303){
                if(theV_3==0){
                    var saacarband1:SAACARBAND = new SAACARBAND();
                    addChild(saacarband1);
                    var fcbkbtn:facebook_btn = new facebook_btn();					
                    fcbkbtn.x=219;
                    fcbkbtn.y=400;
                    var facebookUrl:URLRequest = new URLRequest("http://www.facebook.com/share.php?u=http%3A%2F%2Far.saintaside.com");
                    fcbkbtn.addEventListener(MouseEvent.CLICK, goFacebook);
                    function goFacebook (e:MouseEvent):void{
                    navigateToURL(facebookUrl, "_blank");
                    }
                    var twtrbtn:twitter_btn = new twitter_btn();
                    twtrbtn.x=263;
                    twtrbtn.y=400;
                    var twitterUrl:URLRequest = new URLRequest("http://twitter.com/share?url=http%3A%2F%2Far.saintaside.com&via=AddThis&text=Come%20to%20Me%20-%20Augmented%20Reality%20Musicvideo&");
                    twtrbtn.addEventListener(MouseEvent.CLICK, goTwitter);
                    function goTwitter (e:MouseEvent):void{
                    navigateToURL(twitterUrl, "_blank");
                    }
                    var mspcbtn:myspace_btn = new myspace_btn();
                    mspcbtn.x=305;
                    mspcbtn.y=400;
                    var myspaceUrl:URLRequest = new URLRequest("http://www.myspace.com/Modules/PostTo/Pages/?u=http%3A%2F%2Far.saintaside.com");
                    mspcbtn.addEventListener(MouseEvent.CLICK, goMyspace);
                    function goMyspace (e:MouseEvent):void{
                    navigateToURL(myspaceUrl, "_blank");
                    }
                    var stdvzbtn:studivz_btn = new studivz_btn();
                    stdvzbtn.x=347;
                    stdvzbtn.y=400;
                    var studivzUrl:URLRequest = new URLRequest("http://www.studivz.net/Link/Share/?url=http%3A%2F%2Far.saintaside.com&desc=Come+to+Me+-+Augmented+Reality+Musicvideo&prov=addthis.com");
                    stdvzbtn.addEventListener(MouseEvent.CLICK, goStudivz);
                    function goStudivz (e:MouseEvent):void{
                    navigateToURL(studivzUrl, "_blank");
                    }
                    var lstfmbtn:lastfm_btn = new lastfm_btn();
                    lstfmbtn.x=389;
                    lstfmbtn.y=400;
                    var lastfmUrl:URLRequest = new URLRequest("http://www.lastfm.de/music/Saint+Aside");
                    lstfmbtn.addEventListener(MouseEvent.CLICK, goLastfm);
                    function goLastfm (e:MouseEvent):void{
                    navigateToURL(lastfmUrl, "_blank");
                    }
                    var rplbtn:replay_btn = new replay_btn();
                    rplbtn.x=255.85;
                    rplbtn.y=229.00;
                    var replayUrl:URLRequest = new URLRequest("javascript:location.reload(true)");
                    rplbtn.addEventListener(MouseEvent.CLICK, goReplay);
                    function goReplay (e:MouseEvent):void{
                    navigateToURL(replayUrl, "_self");
                    }
                    Tweener.addTween(headLights3,{time:1,delay:12,onComplete:function() {netStream.pause();video.clear();}});
                    Tweener.addTween(headLights3,{time:1,delay:7,onComplete:function() {_modelWRAP.removeChild( screenMc );_modelWRAP.removeChild( _amps );_modelWRAP.removeChild( _cube );_modelWRAP.removeChild( AdOneMc );_modelWRAP.removeChild( AdTwoMc );_markerNode.removeChild( _modelWRAP );removeEventListener(Event.INIT, _onInit);addChild(rplbtn);addChild(mspcbtn);addChild(twtrbtn);addChild(fcbkbtn);addChild(stdvzbtn);addChild(lstfmbtn);}});
                theV_3 = theV_3+1;
                trace(theV_3);
                }
            }
            var mov2:SAACARMC = screenMcMat.movie as SAACARMC;
            if (netStream.time>38){
            mov2.lyrics();
            }
            if (netStream.time>41){
            mov2.lyrics2();
            }
            if (netStream.time>44){
            mov2.lyrics3();
            }
            if (netStream.time>51){
            mov2.lyrics4();
            }
            if (netStream.time>54){
            mov2.lyrics5();
            }
            if (netStream.time>57){
            mov2.lyrics6();
            }
            if (netStream.time>63){
            mov2.lyrics34();
            }
            if (netStream.time>65){
            mov2.lyrics7();
            }
            if (netStream.time>71){
            mov2.lyrics8();
            }
            if (netStream.time>78){
            mov2.lyrics9();
            }
            if (netStream.time>84){
            mov2.lyrics10();
            }
            if (netStream.time>86){
            mov2.lyrics34();
            }
            if (netStream.time>102){
            mov2.lyrics11();
            }
            if (netStream.time>105){
            mov2.lyrics12();
            }
            if (netStream.time>108.5){
            mov2.lyrics13();
            }
            if (netStream.time>115){
            mov2.lyrics14();
            }
            if (netStream.time>121){
            mov2.lyrics15();
            }
            if (netStream.time>128){
            mov2.lyrics34();
            }
            if (netStream.time>129){
            mov2.lyrics16();
            }
            if (netStream.time>135){
            mov2.lyrics17();
            }
            if (netStream.time>141){
            mov2.lyrics18();
            }
            if (netStream.time>147){
            mov2.lyrics19();
            }
            if (netStream.time>151){
            mov2.lyrics20();
            }
            if (netStream.time>153){
            mov2.lyrics34();
            }
            if (netStream.time>156){
            mov2.lyrics21();
            }
            if (netStream.time>159){
            mov2.lyrics22();
            }
            if (netStream.time>169){
            mov2.lyrics23();
            }
            if (netStream.time>179){
            mov2.lyrics24();
            }
            if (netStream.time>191){
            mov2.lyrics34();
            }
            if (netStream.time>205){
            mov2.lyrics25();
            }
            if (netStream.time>212){
            mov2.lyrics26();
            }
            if (netStream.time>218){
            mov2.lyrics27();
            }
            if (netStream.time>225){
            mov2.lyrics28();
            }
            if (netStream.time>228){
            mov2.lyrics29();
            }
            if (netStream.time>232){
            mov2.lyrics34();
            }
            if (netStream.time>257){
            mov2.lyrics30();
            }
            if (netStream.time>263){
            mov2.lyrics31();
            }
            if (netStream.time>270){
            mov2.lyrics32();
            }
            if (netStream.time>276){
            mov2.lyrics33();
            }
        }
        private function AdOneMcClick(e:MouseEvent):void
        {
            navigateToURL(new URLRequest("http://www.saintaside.com"));
        }
        private function AdTwoMcClick(e:MouseEvent):void
        {
            navigateToURL(new URLRequest("http://www.lastfm.de/music/Saint+Aside"));
        }
        public function start() : void {
            this.addEventListener( MarkerEvent.MARKER_ADDED, this.onMarkerAdded );
            this.addEventListener( MarkerEvent.MARKER_UPDATED, this.onMarkerUpdated );
            this.addEventListener( MarkerEvent.MARKER_REMOVED, this.onMarkerRemoved );
            this.addEventListener( Event.ENTER_FRAME, this._onEnterFrame );
        }
        private function _ChildAddWrapModel(e:Event):void {
            if (theV_2 == 4663) {
                trace(theV_2);
                this._modelWRAP.addChild( _amps );
                theV_2 = 4664;
            }
        }
        private function _headLightTweens(e:Event):void {
            if (theV_2 == 4664) {
                Tweener.addTween(headLights3,{alpha:1,time:1,transition:"linear"});
                Tweener.addTween(headLights2,{alpha:0,time:1,transition:"linear",onComplete:function() {addEventListener(Event.ENTER_FRAME, _headLightTweens2);}});
                theV_2 = theV_2+1;
            } else {removeEventListener(Event.ENTER_FRAME, _headLightTweens2);
            }
            
        }
        private function _headLightTweens2(e:Event):void {
            if (theV_2 == 4665) {
                removeEventListener(Event.ENTER_FRAME, _headLightTweens);
                Tweener.addTween(headLights2,{alpha:1,time:1,transition:"linear"});
                Tweener.addTween(headLights3,{alpha:0,time:1,transition:"linear",onComplete:function() {addEventListener(Event.ENTER_FRAME, _headLightTweens);}});
                theV_2 = theV_2-1;
            } else {removeEventListener(Event.ENTER_FRAME, _headLightTweens);
            }
        }
        public function onMarkerAdded(e : Event= null) : void {
            addEventListener(Event.ENTER_FRAME, function(event:Event):void {
            counta = counta + 1;
                if (counta <= 1) {
                        if (counta == 1) {
                        removeEventListener(Event.ENTER_FRAME, arguments.callee);
                    }
                }
                else {
                    }
                });
            for (theV_1;theV_1 < 1; theV_1++) {
                trace(theV_1);
                var my_Song:Sound = new Sound();
                my_Song.load(new URLRequest("SAACcontent/warp.mp3"));
                var mov:SAACARMC = screenMcMat.movie as SAACARMC;
                var mov3:SAACARAD1MC = AdOneMcMat.movie as SAACARAD1MC;
                var mov4:SAACARAD2MC = AdTwoMcMat.movie as SAACARAD2MC;
                Tweener.addTween(AdOneLink,{alpha:0,time:0});
                Tweener.addTween(AdOneLink,{alpha:1,time:7,delay:27});
                Tweener.addTween(AdOneLink,{time:1,delay:27,onComplete:function(){mov3.setoff();}});
                Tweener.addTween(AdOneLink,{time:1,delay:37,onComplete:function(){mov3.setonmid();}});
                Tweener.addTween(AdTwoLink,{alpha:0,time:0});
                Tweener.addTween(AdTwoLink,{alpha:1,time:7,delay:27});
                Tweener.addTween(AdTwoLink,{time:1,delay:27,onComplete:function(){mov4.setitoff();}});
                Tweener.addTween(AdTwoLink,{time:1,delay:37,onComplete:function(){mov4.setiton();}});
                Tweener.addTween(markerCube,{alpha:0,time:0});
                Tweener.addTween(markerCube, { time:1, transition:'easeinoutexpo',alpha:1,_Glow_alpha:0.8,_Glow_color:0xffffff,_Glow_blurX:50,_Glow_blurY:50,_Glow_strength:0.6});
                Tweener.addTween(markerCube, { time:1,delay:1,onComplete:function(){_amps.play("clip1", false);addEventListener(Event.ENTER_FRAME, _ChildAddWrapModel);}});
                Tweener.addTween(markerCube, { time:1,delay:2,onComplete:function(){my_Song.play();}});
                Tweener.addTween(markerCube, { time:4, transition:'easeinoutexpo',delay:1,alpha:0});
                Tweener.addTween(_amps, { time:1, transition:'easeinoutexpo', scaleX: 0.5, scaleY: 0.5, scaleZ: 0.5,x:-95,y:110,delay:3, onComplete:function(){mov.animate();}});
                Tweener.addTween(headLights,{alpha:0,time:0,transition:"linear"});
                Tweener.addTween(headLights,{alpha:1,_Glow_alpha:0.8,_Glow_color:0xffffff,_Glow_blurX:70,_Glow_blurY:70,_Glow_strength:1,time:3,delay:7,transition:"linear"});
                Tweener.addTween(headLights2,{alpha:0,time:0,transition:"linear"});
                Tweener.addTween(headLights2,{alpha:1,_Glow_alpha:0.8,_Glow_color:0xffffff,_Glow_blurX:70,_Glow_blurY:70,_Glow_strength:1,time:3,delay:7,transition:"linear"});
                Tweener.addTween(headLights3,{alpha:0,time:0,transition:"linear"});
                Tweener.addTween(headLights3,{alpha:1,_Glow_alpha:0.8,_Glow_color:0xffffff,_Glow_blurX:70,_Glow_blurY:70,_Glow_strength:1,time:3,delay:7,transition:"linear",onComplete:function() {addEventListener(Event.ENTER_FRAME, _headLightTweens);}});
                Tweener.addTween(restOfStage,{_brightness:-1.0,time:0,delay:0});
                Tweener.addTween(restOfStage,{_brightness:0,time:3,delay:8,transition:"linear"});
                Tweener.addTween(theWall,{_brightness:-1.5,time:0,delay:0});
                Tweener.addTween(theWall,{_brightness:0,time:3,delay:8,transition:"linear"});
                Tweener.addTween(this, {delay:7, onComplete:function() {netStream.resume();}});
            }
        }
        public function onMarkerUpdated(e : Event= null) : void {
        }
        public function onMarkerRemoved(e : Event= null) : void {
            if (theV_1 == 1) {
                trace(theV_1);
                theV_1 = 2;
            }
        }
        private function _onEnterFrame(e:Event = null):void {
        _capture.bitmapData.draw(_video);
        var detected:Boolean = false;
        try {
            detected = _detector.detectMarkerLite(_raster, 80) && _detector.getConfidence() > 0.5; //80
                    }catch (e:Error) {}
            if (detected) {
                _detector.getTransformMatrix(transformationResult);
                var m:Matrix3D = _markerNode.transform;
                var r:FLARTransMatResult = transformationResult;
                var speed:Number = 0.9;// je höher desto weniger easing
                m.n11 +=  (r.m01 - m.n11) * speed; 
                m.n12 +=  (r.m00 - m.n12) * speed; 
                m.n13 +=  (r.m02 - m.n13) * speed; 
                m.n14 +=  (r.m03 - m.n14) * speed;
                m.n21 += (-r.m11 - m.n21) * speed; 
                m.n22 += (-r.m10 - m.n22) * speed; 
                m.n23 += (-r.m12 - m.n23) * speed; 
                m.n24 += (-r.m13 - m.n24) * speed;
                m.n31 +=  (r.m21 - m.n31) * speed; 
                m.n32 +=  (r.m20 - m.n32) * speed; 
                m.n33 +=  (r.m22 - m.n33) * speed; 
                m.n34 +=  (r.m23 - m.n34) * speed;
                this.dispatchEvent( new MarkerEvent( MarkerEvent.MARKER_ADDED ) );
            } else {
                this.dispatchEvent( new MarkerEvent( MarkerEvent.MARKER_REMOVED ) );
            }
        _renderer.render();
        }
        public function set mirror(value:Boolean):void {
            if (value) {
                _base.scaleX = -1;
                _base.x = 640;
            } else {
                _base.scaleX = 1;
                _base.x = 0;
            }
        }
        public function get mirror():Boolean {
        return _base.scaleX < 0;
        }
    }
}
import flash.events.Event;
class MarkerEvent extends Event {
    public static const MARKER_ADDED : String= "markerAdded";
    public static const MARKER_UPDATED : String= "markerUpdated";
    public static const MARKER_REMOVED : String= "markerRemoved";
    public function MarkerEvent(type : String, bubbles : Boolean= false, cancelable : Boolean= false) {
        super( type, bubbles, cancelable );
    }
}